package com.ksyun.alert.controller;

import com.ksyun.alert.dto.*;
import com.ksyun.alert.result.PageResult;
import com.ksyun.alert.result.Result;
import com.ksyun.alert.service.AlertRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 告警记录控制层
 * @author Zhang
 * @date 2024-04-30
 */
@RestController
@RequestMapping(value = {"/records","api/alert"})
@Api(tags = "查询主机告警信息API")
public class AlertRecordsController {

    @Resource
    private AlertRecordsService alertRecordsService;

    /**
     * 查询主机告警信息API
     * @param pageQueryDTO 查询参数
     */
    @PostMapping
    @ApiOperation(value = "查询主机告警信息API")
    public Result<PageResult> page(@RequestBody @Validated PageQueryDTO pageQueryDTO) {
        PageResult pageResult = alertRecordsService.selectPageByConditions(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询主机告警信息API
     * @param id 删除的告警记录的id参数
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除主机告警记录API")
    public Result<String> deleteByIds(@PathVariable @NotNull(message = "id不能为空") Long id) {
        boolean removeById = alertRecordsService.removeById(id);
        if (!removeById) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 图标数据更新API
     * @param timeStartEndDTO 业务时间区间参数
     */
    @ApiOperation(value = "图标数据更新API")
    @PostMapping("/getByTimeList")
    public Result<List<RecordTime2CountDTO>> selectRecordTime2Counts(@RequestBody @Validated TimeStartEndDTO timeStartEndDTO) {
        List<RecordTime2CountDTO> list = alertRecordsService.selectRecordTime2Counts(timeStartEndDTO);
        return Result.success(list);
    }

    /**
     * 查询告警数据API
     * @param alertQueryReqDTO 查询告警记录请求参数
     */
    @ApiOperation(value = "查询告警数据API")
    @PostMapping("/query")
    public Result<AlertQueryRespDTO> queryAlertsByPeriodAndReqId(@RequestBody AlertQueryReqDTO alertQueryReqDTO) {
        return Result.success(alertRecordsService.queryAlertsByPeriodAndReqId(alertQueryReqDTO));
    }
}
